﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using BUS;
using DTO;
using DAO;
using System.Data.OleDb;


namespace CuaHangHoa
{
    public partial class frmDatHang : Form
    {




        DatHangBUS dhBUS = new DatHangBUS();
        MatHang_DTO mhDTO = new MatHang_DTO();
        HoaDonDTO hdDTO = new HoaDonDTO();
        KhachHangDTO khDTO = new KhachHangDTO();
        List<MatHang_DTO> m_arrDH;

        public frmDatHang()
        {
            InitializeComponent();
        }

        private void QuanLyDatHang_Load(object sender, EventArgs e)
        {
            LoadDSHoa();
           // dtpNgayGiao.Value = DateTime.Today;
            cbLoai.Items.Add("Hoa");
            cbLoai.Items.Add("Chậu Hoa");
            cbLoai.SelectedIndex = 0;
            
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
        public bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }


        private bool kiemTraNhap()
        {
            if (!(txtTenKhachHang.Text == "" && txtTenNguoiNhan.Text == "" && txtSDTKhachHang.Text == "" && txtSDTNguoiNhan.Text == "" &&
                txtGiaoDen.Text == "" && DateTime.Compare(dtpNgayGiao.Value, DateTime.Today) >= 0 && txtTraTruoc.Text == "" && txtTongTien.Text == "" &&
                txtConNo.Text == "" && IsNumber(txtSDTKhachHang.Text) == true || IsNumber(txtSDTNguoiNhan.Text) == true &&
                IsNumber(txtTraTruoc.Text) == true && IsNumber(txtTongTien.Text) == true && IsNumber(txtConNo.Text) == true))
            {

                return false;
            }
            return true;
        }

        private void LoadDSHoa()
        {
            DatHangBUS qldhBUS = new DatHangBUS();
            DataTable dt;
            dt = qldhBUS.layBangMatHang();

            dgvDSHoa.DataSource = dt;
        }

        private void luuTTHoaDon()
        {

            hdDTO.NgayLap = DateTime.Today.ToShortDateString();
            hdDTO.TenNguoiNhan = txtTenNguoiNhan.Text;
            hdDTO.DienThoai = int.Parse(txtSDTNguoiNhan.Text);
            hdDTO.DiaChiGiao = txtGiaoDen.Text;
            hdDTO.NgayGiao = dtpNgayGiao.Value;
            hdDTO.TraTruoc = int.Parse(txtTraTruoc.Text);
            hdDTO.ConNo = int.Parse(txtConNo.Text);
            


        }
        private void luuDSHang()
        {
            m_arrDH = new List<MatHang_DTO>() ;

            for (int i = 0; i < lvDSHang.Items.Count - 1;i++ )
            {
                mhDTO = new MatHang_DTO();
                mhDTO.TenHang = lvDSHang.Items[i].SubItems[0].Text;
                mhDTO.SoLuong = int.Parse(lvDSHang.Items[i].SubItems[1].Text);
                
            }
        }
        private void luuTTKhachHang()
        {
            khDTO.TenKhachHang = txtTenKhachHang.Text;
            khDTO.SoDTKhachHang = txtSDTKhachHang.Text;
        }



        private void btnDatHang_Click(object sender, EventArgs e)
        {


            if (!kiemTraNhap())
            {
                MessageBox.Show("Dữ liệu nhập vào chưa đúng, vui lòng kiểm tra lại.");
            }
            else
            {
                luuTTHoaDon();
                luuTTKhachHang();
                int idhoadon,kq1,kq2,kq3;
                int idkhachang = int.Parse(dhBUS.layIDKhachHangMax().ToString());//lấy mã khách hàng MAX
                hdDTO.MaKhachHang = idkhachang + 1;
               
                kq1 = dhBUS.themKhachHang(khDTO);
                kq2 = dhBUS.themHoaDon(hdDTO);

                idhoadon = dhBUS.layIDHoaDonMax() + 1;
                luuDSHang();
               kq3 = dhBUS.themChiTietHoaDon(mhDTO, idhoadon);
                if ((kq2 = kq3 = kq1) == 1)
                {
                    MessageBox.Show("Them thanh cong");
                }
                else
                {
                    MessageBox.Show("Them that bai");
                }



            }

        }

       
        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            txtTenKhachHang.Text = "" ;
            txtTenNguoiNhan.Text = "" ;
            txtSDTKhachHang.Text = "" ;
            txtSDTNguoiNhan.Text ="" ;
            txtGiaoDen.Text = "" ;
            dtpNgayGiao.Value = DateTime.Today ;
            txtTraTruoc.Text = "" ;
            txtTongTien.Text = "" ;
            txtConNo.Text = "" ;
            txtGhiChu.Text = "" ;
            lvDSHang.Items.Clear();
        }


        private void ThemSPVaoListView(MatHang_DTO mhDTO)
        {
            ListViewItem lvi = lvDSHang.Items.Add(mhDTO.TenHang);
            lvi.SubItems.Add("1");
            lvi.SubItems.Add("cành");
            lvi.SubItems.Add(mhDTO.DonGia.ToString());
            lvi.SubItems.Add(mhDTO.DonGia.ToString());
        }

        private bool KiemTraThem(MatHang_DTO mhDTO)
        {
            for (int i = 0; i <= lvDSHang.Items.Count - 1;i++ )
            {
                if (string.Compare(lvDSHang.Items[i].SubItems[0].Text, mhDTO.TenHang) == 0)
                {
                    return false;
                }
                
            }
            return true;
        }

        private void dgvDSHoa_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvDSHoa.SelectedCells.Count > 0 && !(this.dgvDSHoa.SelectedCells[0].RowIndex == this.dgvDSHoa.RowCount - 1))
            {

                int dong = this.dgvDSHoa.SelectedCells[0].RowIndex;// lay chi so dong o dc chon
                mhDTO.TenHang = dgvDSHoa[0, dong].Value.ToString();
                mhDTO.DonGia = int.Parse(dgvDSHoa[1, dong].Value.ToString());
                if (KiemTraThem(mhDTO))
                {
                    ThemSPVaoListView(mhDTO);
                }
                else                
                {
                    MessageBox.Show("Bạn đã chọn loại hoa này rồi.", "Thông Báo");
                }
            }
        }

        private void btnCong_Click(object sender, EventArgs e)
        {
            if (lvDSHang.SelectedIndices.Count > 0)
            {
                int index = lvDSHang.SelectedIndices[0];
                int soluong = int.Parse(lvDSHang.Items[index].SubItems[1].Text) + 1;//lay so luong và +1
                int dongia = int.Parse(lvDSHang.Items[index].SubItems[3].Text);// lấy don gia
                string sl = soluong.ToString();//trang so luong len 1
                lvDSHang.Items[index].SubItems[1].Text = sl;//gan lai
                lvDSHang.Items[index].SubItems[4].Text = (soluong*dongia).ToString();//gan lai thành tien

            }
        }

        private void btnGiam_Click(object sender, EventArgs e)
        {
            if (lvDSHang.SelectedIndices.Count > 0)
            {
                int index = lvDSHang.SelectedIndices[0];
                int soluong = int.Parse(lvDSHang.Items[index].SubItems[1].Text);
               
                if (soluong > 1)
                {
                    int dongia = int.Parse(lvDSHang.Items[index].SubItems[3].Text);// lấy don gia
                    string sl = (soluong-1).ToString();//giam so luong  1
                    lvDSHang.Items[index].SubItems[1].Text = sl;//gan lai
                    lvDSHang.Items[index].SubItems[4].Text = ((soluong-1) * dongia).ToString();//gan lai thành tien
                }
               
                else
                {
                    lvDSHang.Items.RemoveAt(index);
                }


            } 
        }

        private void cbLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            DatHangBUS dhBUS = new DatHangBUS();
            DataTable dt = new DataTable();
            if (cbLoai.SelectedIndex == 0)
            {
                dt = dhBUS.laySPTheoLoai(1);
                dgvDSHoa.DataSource = dt;
            }
            else {
                dt = dhBUS.laySPTheoLoai(2);
                dgvDSHoa.DataSource = dt;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(dhBUS.layIDKhachHangMax().ToString());
            //DataTable dt = new DataTable();
            //OleDbConnection cn = DataProvider.ConnectDB();
            //string str = "Select MAX(MaKhachHang) as Max from KHACHHANG";
            //OleDbDataAdapter da = new OleDbDataAdapter( str, cn);
            //da.Fill(dt);
            //cn.Close();
            //dgvt.DataSource = dt;
        }

        private void btnTongTien_Click(object sender, EventArgs e)
        {
            int tongtien = 0;
            for (int i = 0; i <= lvDSHang.Items.Count - 1; i++)
            {
                tongtien += int.Parse(lvDSHang.Items[i].SubItems[4].Text);

            }
            txtTongTien.Text = tongtien.ToString();
        }

        private void btnConNo_Click(object sender, EventArgs e)
        {
            int conno = 0;
           
            if (txtTongTien.Text == "")
            {
                return;
            }
            else
            {
                int tongtien, tratruoc, tiengiam;
                tongtien = int.Parse(txtTongTien.Text);
                if (txtTraTruoc.Text == "")
                {
                    tratruoc = 0;
                }
                else {
                    tratruoc = int.Parse(txtTraTruoc.Text);
                }
                if (txtTienGiam.Text == "")
                {
                    tiengiam = 0;
                }
                else {
                    tiengiam = int.Parse(txtTienGiam.Text);
                }

                conno = tongtien - (tiengiam + tratruoc);
            }
           
            txtConNo.Text = conno.ToString();
        }

        private void btnQuanLy_Click(object sender, EventArgs e)
        {
            frmQuanLyDonDatHang frm = new frmQuanLyDonDatHang();
            frm.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(dtpNgayGiao.Value.ToString() + DateTime.Today.ToString() + DateTime.Now.ToString());
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            MessageBox.Show(dtpNgayGiao.Value.Year.ToString());
        }


     
    }
       
}
